package com.dikang.yygh.controller;

import com.alibaba.excel.util.StringUtils;
import com.dikang.yygh.common.result.Result;
import com.dikang.yygh.model.cmn.Dict;
import com.dikang.yygh.service.DictService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(value = "/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    /**
     * 查询子数据信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        return Result.ok(dictService.findChildData(id));

    }

    /**
     * 导出数据
     *
     * @param response
     */
    @GetMapping(value = "exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }


    @PostMapping(value = "importData")
    public Result importData(MultipartFile file) {
        System.out.println(file.getContentType());
        System.out.println(file.getSize());
        System.out.println(file.getOriginalFilename());
        try {
            System.out.println(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        dictService.importData(file);
        return Result.ok();
    }

    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value){
        String dictName = dictService.findDictName(dictCode, value);
        return dictName;
    }

    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value){
        String dictName = dictService.findDictName("", value);
        return dictName;
    }
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(
            @PathVariable String dictCode
    ){
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }
}
